import {
	apiBaseUrl
} from './config.js';
import * as common from './common.js' //引入common
import * as db from './db.js' //引入common
// 需要登陆的，都写到这里，否则就是不需要登陆的接口
const methodsToken = ['api/wx/decryptphone'];


const post = (method, data, callback, complete) => {
	uni.removeStorage({
		key: 'jump'
	})
	// 获取用户token
	let headers = {
		'Accept': 'application/json',
		'Content-Type': 'application/x-www-form-urlencoded'
	}

	// if () {
	// 	headers = {
	// 	'Accept': 'application/json',
	// 	'Content-Type': 'application/json',
	// 	}
	// }
	let userToken = db.get("userToken");
	let openid = db.get("openid");
	if (userToken) {
		headers.Authorization = userToken
	}
	if (openid) {
		headers.openid = openid
	}

	let postData = JSON.parse(JSON.stringify(data))
	delete postData.methodType
	uni.request({
		url: apiBaseUrl + method,
		data: postData,
		header: headers,
		method: data.methodType || 'post',
		success: (response) => {
			const result = response.data
			if (!result.status) {
				// 登录信息过期或者未登录
				if (result.code === 201) {
					db.del("userToken");
					let jump = uni.getStorageSync('jump')
					console.log(jump, 1111)
					if (!jump) {
						uni.setStorageSync('jump', true)
						uni.showToast({
							title: result.message,
							icon: 'none',
							duration: 1500
						});
						setTimeout(() => {

							uni.navigateTo({
								url: '/pages/login/index'
							})
						}, 1500)
					}
				}
			}
			callback(result);
		},

		complete: (response) => {
			setTimeout(function() {
				// uni.hideLoading();
			}, 1000)
			complete ? complete() : "";
		},
		fail: (error) => {
			// uni.showLoading({
			// 	title: '网络开小差了'
			// });
			// setTimeout(function() {
			// uni.hideLoading();
			// }, 1000)
			if (error && error.response) {
				showError(error.response);
			} else {

			}

		},
	});

}
const get = (url, callback) => {
	uni.showLoading({
		title: '加载中'
	});
	uni.request({
		url: url,
		header: {
			'Accept': 'application/json',
			'Content-Type': 'application/x-www-form-urlencoded', //自定义请求头信息
		},
		method: 'GET',
		success: (response) => {
			// uni.hideLoading();
			callback(response.data);
		},
		fail: (error) => {
			// uni.hideLoading();
			if (error && error.response) {
				showError(error.response);
			}
		},
		complete: () => {
			setTimeout(function() {
				// uni.hideLoading();
			}, 250);
		}
	});
}

const showError = error => {
	let errorMsg = ''
	switch (error.status) {
		case 400:
			errorMsg = '请求参数错误'
			break
		case 401:
			errorMsg = '未授权，请登录'
			break
		case 403:
			errorMsg = '跨域拒绝访问'
			break
		case 404:
			errorMsg = `请求地址出错: ${error.config.url}`
			break
		case 408:
			errorMsg = '请求超时'
			break
		case 500:
			errorMsg = '服务器内部错误'
			break
		case 501:
			errorMsg = '服务未实现'
			break
		case 502:
			errorMsg = '网关错误'
			break
		case 503:
			errorMsg = '服务不可用'
			break
		case 504:
			errorMsg = '网关超时'
			break
		case 505:
			errorMsg = 'HTTP版本不受支持'
			break
		default:
			errorMsg = error.msg
			break
	}

	uni.showToast({
		title: errorMsg,
		icon: 'none',
		duration: 1000,
		complete: function() {
			setTimeout(function() {
				uni.hideToast();
			}, 1000);
		}
	});
}

// 文件上传
export const uploadFiles = (callback) => {
	uni.chooseImage({
		success: (chooseImageRes) => {
			uni.showLoading({
				title: '上传中...'
			});
			const tempFilePaths = chooseImageRes.tempFilePaths;
			const uploadTask = uni.uploadFile({
				url: apiBaseUrl + 'api/file/upload', //仅为示例，非真实的接口地址
				filePath: tempFilePaths[0],
				fileType: 'image',
				name: 'file',
				header: {

					// 'Accept': 'application/json',
					// 'Content-Type': 'application/x-www-form-urlencoded',
					'Authorization': db.get("userToken")
				},
				formData: {
					'file': tempFilePaths[0],

				},
				success: (uploadFileRes) => {
					callback(JSON.parse(uploadFileRes.data));
				},
				fail: (error) => {
					if (error && error.response) {
						showError(error.response);
					}
				},
				complete: () => {
					setTimeout(function() {
						uni.hideLoading();
					}, 250);
				}
			});
		}
	});
}

// 登录
export const wxLogin = (data, callback) => post('api/wx/login', data, callback);
export const memberInfo = (data, callback) => post('api/member/info', data, callback);
export const vehicleList = (data, callback) => post('api/member/vehicle_list', data, callback);
export const vehicleSave = (data, callback) => post('api/member/save_vehicle', data, callback);
export const vehicleEdit = (data, callback) => post('api/member/update_vehicle', data, callback);
export const vehicleInfo = (data, callback) => post('api/member/vehicle_detail', data, callback);
export const myAppointInfo = (data, callback) => post('api/member/appoint_detai', data, callback);

export const vehicleDel = (data, callback) => post('api/member/delete_vehicle', data, callback);
export const appointList = (data, callback) => post('api/appoint_active/list', data, callback);
export const appointInfo = (data, callback) => post('api/appoint_active/detail', data, callback);
export const appointSave = (data, callback) => post('api/member/save_appoint', data, callback);
export const appointCancel = (data, callback) => post('api/member/cancel_appoint', data, callback);
export const appointSign = (data, callback) => post('api/member/sign_appoint', data, callback);
export const myAppointList = (data, callback) => post('api/member/appoint_list', data, callback);
export const adList = (data, callback) => post('api/ad/list', data, callback);
export const articleList = (data, callback) => post('api/article/list', data, callback);
export const articleInfo = (data, callback) => post('api/article/detail', data, callback);
export const dictList = (data, callback) => post('api/dict-data/list', data, callback);
export const questionInfo = (data, callback) => post('api/question/info', data, callback);
export const questionTopic= (data, callback) => post('api/question/topic', data, callback);
export const userUpdate= (data, callback) => post('api/member/update', data, callback);
export const submit_question= (data, callback) => post('api/member/submit_question', data, callback);

// 微信获取手机号码
export const wxPhone = (data, callback) => post('api/wx/decryptphone', data, callback);

export const userUpdateMobile = (data, callback) => post('api/member/update_mobile', data, callback);

export const appoint_detail = (data, callback) => post('api/member/appoint_detail', data, callback);
export const productSearch = (data, callback) => post('api/product/search', data, callback);
export const productInfo = (data, callback) => post('api/product/detail', data, callback);
export const categoryAll = (data, callback) => post('api/category/list', data, callback);

// 地址管理
export const getAreaList = (data, callback) => post('api/area/tree', data, callback);
export const addressCreate = (data, callback) => post('api/address/create', data, callback);
export const addressUpdate = (data, callback) => post('api/address/update', data, callback);
export const addressDelete = (data, callback) => post('api/address/delete', data, callback);
export const addressGet = (data, callback) => post('api/address/get', data, callback);
export const addressGetDefault = (data, callback) => post('api/address/get-default', data, callback);
export const addressList = (data, callback) => post('api/address/list', data, callback);

// 购物车管理


export const buyNow = (data, callback) => post('/api/cart/now-buy', data, callback);
export const cartList = (data, callback) => post('/api/cart/list', data, callback);
export const cartAdd = (data, callback) => post('/api/cart/add', data, callback);
export const cartUpdateCount = (data, callback) => post('/api/cart/update-count', data, callback);
export const cartUpdateSelected = (data, callback) => post('/api/cart/update-selected', data, callback);
export const cartDelete = (data, callback) => post('/api/cart/delete', data, callback);
export const cartGetCount = (data, callback) => post('/api/cart/get-count', data, callback);

// 订单结算

export const orderCheckSettlement = (data, callback) => post('/api/trade/order/check_settlement', data, callback);
export const expressTemplate = (data, callback) => post('/api/trade/order/express/template/list', data, callback);
export const orderSettlement = (data, callback) => post('/api/trade/order/settlement', data, callback);
export const orderCreate = (data, callback) => post('/api/trade/order/create', data, callback);
export const orderList = (data, callback) => post('/api/trade/order/page', data, callback);
export const orderPay = (data, callback) => post('/api/trade/order/pay/submit', data, callback);
export const orderInfo = (data, callback) => post('/api/trade/order/get-detail', data, callback);
export const updatePaid = (data, callback) => post('/api/trade/order/update-paid', data, callback);
export const expressList = (data, callback) => post('/api/trade/order/get-express-track-list', data, callback);
export const orderCount = (data, callback) => post('/api/trade/order/get-count', data, callback);
export const orderReceive = (data, callback) => post('/api/trade/order/receive', data, callback);
export const orderCancel = (data, callback) => post('/api/trade/order/cancel', data, callback);
export const orderDeliverSettlement = (data, callback) => post('/api/trade/order/confirm_deliver_settlement', data,
	callback);
export const orderDeliverConfirm = (data, callback) => post('/api/trade/order/confirm_deliver', data, callback);


// 个人中心
export const pointPage = (data, callback) => post('/api/member/point/page', data, callback);
export const rainbowPage = (data, callback) => post('/api/member/rainbow/page', data, callback);
export const signSummary = (data, callback) => post('/api/member/sign/summary', data, callback);
export const signConfig = (data, callback) => post('/api/member/sign/config', data, callback);
export const signCreate = (data, callback) => post('/api/member/sign/create', data, callback);
export const pointExchangeList = (data, callback) => post('/api/member/point/exchange/list', data, callback);
export const pointExchangeAdd = (data, callback) => post('/api/member/point/exchange/add', data, callback);
export const rainbowExchangeList = (data, callback) => post('/api/member/rainbow/exchange/list', data, callback);
export const rainbowExchangeAdd = (data, callback) => post('/api/member/rainbow/exchange/add', data, callback);
export const myCardList = (data, callback) => post('/api/member/card/list', data, callback);
export const couponPage = (data, callback) => post('/api/coupon/page', data, callback);
export const couponList = (data, callback) => post('/api/coupon/template-list', data, callback);
export const couponTake = (data, callback) => post('/api/coupon/take', data, callback);
export const walletRecharge = (data, callback) => post('/api/member/wallet/recharge', data, callback);
export const walletRechargeList = (data, callback) => post('/api/member/wallet/list', data, callback);
export const walletTransactionList = (data, callback) => post('/api/member/wallet-transaction/list', data, callback);
export const productSku = (data, callback) => post('/api/product/sku', data, callback);
export const excellentList = (data, callback) => post('/api/excellent-composition/list', data, callback);
export const communityList = (data, callback) => post('/api/community/list', data, callback);
export const compositionInfo = (data, callback) => post('/api/composition/detail', data, callback);
export const communityReviewList = (data, callback) => post('/api/community-review/list', data, callback);

export const communityReviewCreate = (data, callback) => post('/api/member/community-review/create', data, callback);
export const communityLikeCreate = (data, callback) => post('/api/member/community-like/create', data, callback);
export const communityLikeCancel = (data, callback) => post('/api/member/community-like/cancel', data, callback);
export const communityCollectCreate = (data, callback) => post('/api/member/community-collect/create', data, callback);
export const communityCollectCancel = (data, callback) => post('/api/member/community-collect/cancel', data, callback);

export const sourceMaterialList = (data, callback) => post('/api/sourceMaterial/List', data, callback);

// 商品收藏
export const productCorrectCreate = (data, callback) => post('/api/member/product-collect/create', data, callback);
export const productCorrectCancel = (data, callback) => post('/api/member/product-collect/cancel', data, callback);
export const productCorrectList = (data, callback) => post('/api/member/product-collect/list', data, callback);
export const productCorrectDel = (data, callback) => post('/api/member/product-collect/delete', data, callback);
export const deleteUser = (data, callback) => post('/api/member/deleteUser', data, callback);
// 积分商城
export const pointExchangeConfig = (data, callback) => post('/api/pointExchangeConfig/List', data, callback);
export const exchangeAdd = (data, callback) => post('/api/member/point/exchange/add', data, callback);
export const exchangeList = (data, callback) => post('/api/member/point/exchange/page', data, callback);

export const memberLevel = (data, callback) => post('/api/member/level_info', data, callback);
export const new_user_static = (data, callback) => post('/api/member/new_user_static', data, callback);
export const afterSaleAdd = (data, callback) => post('/api/trade/order/after-sale/create', data, callback);
export const afterSalePage = (data, callback) => post('/api/trade/order/after-sale/page', data, callback);
export const afterSaleCancel = (data, callback) => post('/api/trade/order/after-sale/cancel', data, callback);
export const afterSaleDetail = (data, callback) => post('/api/trade/order/after-sale/detail', data, callback);

export const diyMatting = (data, callback) => post('/api/diy/matting', data, callback);

export const diySwPreview = (data, callback) => post('/api/diy/sw_preview', data, callback);
export const diyCxPreview = (data, callback) => post('/api/diy/cx_preview', data, callback);

export const compositionSave = (data, callback) => post('/api/member/composition/save', data, callback);

export const compositionUpdate = (data, callback) => post('/api/member/composition/update', data, callback);

export const compositionDelete= (data, callback) => post('/api/member/composition/delete', data, callback);

export const compositionList = (data, callback) => post('/api/member/composition/list', data, callback);
export const compositionDetail = (data, callback) => post('/api/member/composition/detail', data, callback);
export const share_composition = (data, callback) => post('/api/member/share_composition', data, callback);
export const communityShareList = (data, callback) => post('/api/member/community-share/list', data, callback);
export const communityCollectList = (data, callback) => post('/api/member/community-collect/list', data, callback);

export const myCommunityLikeList = (data, callback) => post('/api/member/my-community-like/list', data, callback);
export const myCommunityCollectList = (data, callback) => post('/api/member/my-community-collect/list', data, callback);
export const myCommunityReviewList = (data, callback) => post('/api/member/my-community-review/list', data, callback);
export const communityAdd = (data, callback) => post('/api/member/community/add', data, callback);

export const communityInfo = (data, callback) => post('/api/community/detail', data, callback);

export const communityMyList = (data, callback) => post('/api/member/community-my/list', data, callback);

export const communityDel= (data, callback) => post('/api/member/community/delete', data, callback);
export const genFontpic= (data, callback) => post('/api/diy/gen_fontpic', data, callback);
export const msg_sec_check= (data, callback) => post('/api/member/msg_sec_check', data, callback);

